﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MPSizectorDotNet;
using System.Threading;

namespace CameraMPSizector
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

          
        }
        int add = 0;
        public MPSizector Device;
        private System.Windows.Forms.Timer RefreshTimer= new System.Windows.Forms.Timer();
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.RefreshTimer.Enabled = true;
            RefreshTimer.Tick += RefreshTimer_Tick;

          
        }

        private void RefreshTimer_Tick(object sender, EventArgs e)
        {
            lb.Text = "正在连接相机";
            this.Device.UpdateDeviceList();
            int deviceCount = this.Device.GetDeviceCount();
            if (deviceCount > 0)
            {
                MPDeviceInfo deviceInfo = this.Device.GetDeviceInfo(0);
                if (this.Device.Open(deviceInfo))
                {
                    lb.Text = "相机连接成功";
                    base.DialogResult = DialogResult.OK;
                    this.RefreshTimer.Enabled = false;
                    Thread.Sleep(100);
                    this.Close();
                }
                else
                {
                    lb.Text = "相机连接失败、尝试重连";
                }
            }          
            this.RefreshTimer.Interval = 2000;
        }
    }
}
